/**
 * Project Name:spring-config-client File Name:StopEndpoint.java Package Name:org.spring.config.client.controller
 * Date:2019年10月16日下午7:16:24 Copyright (c) 2019, dengqian37@126.com All Rights Reserved.
 *
 */

package org.spring.cloud.pilot.endpoint.mvc;

import java.util.Collections;

import org.spring.cloud.pilot.endpoint.RebootEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
		* ClassName: RebootMvcEndpoint <br/>
		* Function: TODO ADD FUNCTION. <br/>
		* Reason: TODO ADD REASON(可选). <br/>
		* date: 2019年10月23日 下午5:22:30 <br/>
		*
		* @author admin
		* @version 
		* @since JDK 1.8
 */
public class RebootMvcEndpoint extends EndpointMvcAdapter {

    public RebootMvcEndpoint(RebootEndpoint delegate) {

        super(delegate);

    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    @Override
    public Object invoke() {
        if (!getDelegate().isEnabled()) {
            return new ResponseEntity<>(Collections.singletonMap("message", "This endpoint is disabled"),
                HttpStatus.NOT_FOUND);
        }
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500L);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }

                RebootMvcEndpoint.super.invoke();
            }
        });
        thread.setDaemon(false);
        thread.start();

        return Collections.singletonMap("message", "rebootting");
    }

}
